
((_global_)=>{
	if(!Reflect.has(_global_, 'mtec')){
		Reflect.set(_global_, 'mtec', {});
		Reflect.set(mtec, '__mtec__', 'Many technologies');
	}

	if(!mtec.algebra){
		Reflect.set(mtec, 'algebra', {});
	}
	Reflect.set(mtec.algebra, '__space_description__', 'This is a mtec subnamespace for algebra');
})(window ?? globalThis);

mtec.algebra.Straight = class implements mtec.algebra.Straight{
	/**
	 * f(x) = [k]x + b\
	 * 直线的斜率
	 */
	private _k_: number;
	/**
	 * f(x) = kx + [b]\
	 * 直线的截距
	 */
	private _b_: number;

	public constructor(...args: [number, number] | [[number, number], [number, number]]){
		this.reset(...args);
	}

	public vatiable(value: number){
		return this._k_ * value + this._b_;
	}

	public value(value: number){
		if(this._k_ === 0){
			return this._b_;
		}else{
			return (value - this._b_) / this._k_;
		}
	}

	public reset(...args: [number, number] | [[number, number], [number, number]]){
		if(args.map(a=>typeof a).every(t=>t==='number')){
			[this._k_, this._b_] = args as [number, number];
		}else if(args.map(a=>Array.isArray(a)).every(t=>t)){
			let [[x1, y1], [x2, y2]] = args as [[number, number], [number, number]];
			this._k_ = (y2 - y1) / (x2 - x1);
			this._b_ = y1 - this._k_ * x1;
		}else{
			throw new Error('Invalid arguments');
		}
	}
}